# coding = utf-8
from rest_framework.viewsets import ReadOnlyModelViewSet

from wine import settings
from django.db.models import Q

from rest_framework.generics import ListAPIView
from shopping.models import Goods, Category, GiftPackage
from users.api.utils import get_user_by_request
from wxchat.api.permissions import WeixinPermission

from .serializers import CategorySerializer, GoodsSerializer, GiftPackageSerializer, WxGiftPackageSerializer


class CategoryListAPIView(ListAPIView):
    """分类"""
    authentication_classes = ()
    # permission_classes = (WeixinPermission,)
    pagination_class = None
    queryset = Category.objects.filter(is_show=1)
    serializer_class = CategorySerializer


class WxGoodsReadOnlyViewSet(ReadOnlyModelViewSet):
    authentication_classes = ()
    permission_classes = ()
    queryset = Goods.objects.filter(drop=0).order_by('sort')
    serializer_class = GoodsSerializer

    def get_queryset(self):
        queryset = super().get_queryset()

        category_id = self.request.query_params.get("category", None)
        # 查看详细内容
        if self.lookup_field in self.kwargs:
            return queryset

        if category_id:
            queryset = queryset.filter(category_id=int(category_id))

        return queryset


class WxGiftPackageListAPIView(ListAPIView):
    """礼包"""
    authentication_classes = ()
    permission_classes = ()
    pagination_class = None
    queryset = GiftPackage.objects.all()
    serializer_class = WxGiftPackageSerializer

    def get_queryset(self):
        queryset = super().get_queryset()

        code = self.request.query_params.get("code", None)

        if code:

            queryset = queryset.filter(card_no=code)

            return queryset

        return None
